package com.tongxin.van.frameworkcanary.base;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;

/**
 *
 * author: VanyLiu
 * email: 13212674795@163.com
 * description:fragment基类
 *
**/

public abstract class BaseFragment<T extends ViewDataBinding> extends Fragment {

    protected T mBinding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getBundleData(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, layoutId(), container, false);

        initTitlebar();

        initViews();

        loadData();

        return mBinding.getRoot();
    }

    /**
     * 返回布局id
     */
    protected abstract int layoutId();

    /**
     * 获取上个页面传过来的数据
     */
    protected abstract void getBundleData(Bundle bundle);

    /**
     * 初始化标题栏，所有标题栏相关的代码都写在这里
     */
    protected abstract void initTitlebar();

    /**
     * 初始化View（设置控件样式、初始值、设置监听事件等）
     */
    protected abstract void initViews();

    /**
     * 获取数据
     */
    protected abstract void loadData();

    /**
     * 启动Activity
     */
    protected void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }

    /**
     * 启动Activity,带Bundle参数
     */
    protected void startActivity(Class<? extends Activity> cls, @NonNull Bundle bundle) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 启动Activity,带请求码
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode) {
        Intent intent = new Intent(getActivity(), cls);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动Activity,带请求码,带Bundle参数
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode, @NonNull Bundle bundle) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 关闭下拉刷新动画或上拉加载更多动画
     *
     * @param refreshLayout
     */
    protected void onAnimationFinish(@NonNull SmartRefreshLayout refreshLayout) {
        if (refreshLayout.isEnableRefresh()) {
            refreshLayout.finishRefresh();
            refreshLayout.setNoMoreData(false);//恢复上拉状态
        }
        if (refreshLayout.isEnableLoadMore()) {
            refreshLayout.finishLoadMore();
        }
    }
}
